package com.apes.fa.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 资产转出(账套内)
 * @author: PengZiFeng
 * @create: 2021-08-19 17:11
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaAssetsTransferOut.CONST_TABLE_NAME)
public class FaAssetsTransferOut extends Model {

    public static final String CONST_TABLE_NAME = "fa_assets_transfer_out";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "资产转出(账套内)", prefix = "ZN%(company)", padding = 5)
    @Comment("资产变动代码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "send_dept_id", referencedColumnName = "id")
    @Comment("发货部门")
    private Dept sendDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "receipt_dept_id", referencedColumnName = "id")
    @Comment("收货部门")
    private Dept receiptDept;

    @Column(name = "send_goods_state")
    @Comment("发货状态")
    private String sendGoodsState;

    @Column(name = "receipt_goods_state")
    @Comment("收货状态")
    private String receiptGoodsState;

    @Column(name = "state")
    @Comment("状态(create:创建,approve:已审核,partSend:部分发货,partReceipt:部分收货,send:已发货,receipt:已收货)")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @Column(name = "date_approve")
    @Comment("审批日期")
    private Date approveDate;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @OneToMany(mappedBy = "assetsTransferOut", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<FaAssetsTransferOutItem> faAssetsTransferOutItems = new ArrayList<>();

    public List<FaAssetsTransferOutItem> getFaAssetsTransferOutItems() {
        if (faAssetsTransferOutItems.size() == 0) faAssetsTransferOutItems = new ArrayList<>();
        for (FaAssetsTransferOutItem faAssetsTransferOutItem : faAssetsTransferOutItems) {
            faAssetsTransferOutItem.setOldDeptId(sendDept.getId());
            faAssetsTransferOutItem.setOldDeptName(sendDept.getName());
            faAssetsTransferOutItem.setDept(receiptDept);
            faAssetsTransferOutItem.setAssetsTransferOut(this);
        }
        return faAssetsTransferOutItems;
    }

    //ID前缀
    @Transient
    private String company;

    public String getCompany(){
        return sendDept.getCompany().getId();
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }


}
